package classmate.view;

import javax.swing.*;
import classmate.controller.RoomController;
import classmate.po.Room;

public class RoomDialog extends JDialog {
	private static final long serialVersionUID = 1L;
	private RoomController roomController;
	private JTextField roomNoTextField, roomNameTextField;
	private JButton confirmButton;
	private JPanel roomAddPanel;
	private Room room;

	public RoomDialog(StudentFrame owner, String title, Room room) {
		this.roomController = owner.getRoomControllerListener();
		this.roomController.setRoomDialog(this);
		this.setTitle(title);
		this.room = room;
		this.add(getRoomAddPanel());
		this.setModal(true);
		this.setSize(300, 200);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.validate();
	}

	public void closeDialog() {
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}

	private JPanel getRoomAddPanel() {
		roomAddPanel = new JPanel();
		if ("添加宿舍".equals(this.getTitle())) {
			roomNoTextField = new JTextField(16);
			roomNameTextField = new JTextField(16);
			confirmButton = new JButton("确定添加");
			confirmButton.setActionCommand("addRoom");
		} else {
			roomNoTextField = new JTextField(String.valueOf(room.getRoomNo()), 20);
			roomNameTextField = new JTextField(room.getRoomName(), 20);
			confirmButton = new JButton("确定修改");
			confirmButton.setActionCommand("updateRoom");
		}
		confirmButton.addActionListener(roomController);
		roomAddPanel.add(new JLabel("宿舍号："));
		roomAddPanel.add(roomNoTextField);
		roomAddPanel.add(new JLabel("宿舍名："));
		roomAddPanel.add(roomNameTextField);
		roomAddPanel.add(confirmButton);
		return roomAddPanel;
	}

	public JTextField getRoomNoTextField() {
		return roomNoTextField;
	}

	public JTextField getRoomNameTextField() {
		return roomNameTextField;
	}

	public JButton getConfirmButton() {
		return confirmButton;
	}

}
